var cgrk = {
    initQuery:function () {
        $('#cgrk_cgrkw').combotree({
            loadFilter:function (rows) {
                return convert(rows);
            }
        });
        $('#cgrk_dzck').combobox({
            url:$.webPath() + '/organ/ck/ckmcList',
            valueField:'id',
            textField:'mc',
            panelHeight:'200',
            editable:true
        });

        //客户加载
        $("[name='querycgrk.gys']").qyjgzcombobox();
    },
    initDatagrid:function () {
        $("#grid_cgrk").datagrid(cgrk.dataOptions);
    },
    initBtn:function () {
        //查询
        $("#btn_cgrk_search").click(function () {
            $('#grid_cgrk').datagrid({pageNumber:1, queryParams:cgrk.getQuery()});
        });

        // 导入凭证按钮
        $("#btn_cgrk_insert").click(function () {
            cgrk_insert();
        });
        //撤销
        $("#btn_cgrk_cx").click(function () {
            cgrk_cx();
        });
        //重置
        $("#btn_cgrk_cz").click(function () {
            cgrk_cz();
        });

    },
    setBtnStatus:function () {
        var row = $("#grid_cgrk").datagrid("getSelected");
      if (row && row.id) {
        if (row.CZZT == 'wdr') {
            $("#btn_cgrk_insert").show();//导入
            $("#btn_cgrk_cx").hide();//撤销
            $("#btn_cgrk_cz").hide();//重置
        }
        if (row.CZZT == 'drcg') {
            $("#btn_cgrk_insert").hide();//导入
            $("#btn_cgrk_cx").show();//撤销
            $("#btn_cgrk_cz").hide();//重置
        }
        if (row.CZZT == 'drsb') {
            $("#btn_cgrk_insert").hide();//导入
            $("#btn_cgrk_cx").hide();//撤销
            $("#btn_cgrk_cz").show();//重置
        }
        if (row.CZZT == 'sccg') {
            $("#btn_cgrk_insert").show();//导入
            $("#btn_cgrk_cx").hide();//撤销
            $("#btn_cgrk_cz").hide();//重置
        }
        if (row.CZZT == 'scsb') {
            $("#btn_cgrk_insert").hide();//导入
            $("#btn_cgrk_cx").hide();//撤销
            $("#btn_cgrk_cz").show();//重置
        }
       } else {
         $("#btn_cgrk_insert").hide();//导入
         $("#btn_cgrk_cx").hide();//撤销
         $("#btn_cgrk_cz").hide();//重置
       }
    },
    dataOptions:{
        url:$.webPath() + "/cwgl/cgrk/cgrkList_aaa?htType=cgd&&sftzd=false",
        pageNumber:1,
        loadFilter:function (data) {
            $(data.rows).each(function () {
                this.zt_text = this.zt == 'ysp' ? "已分配" :
                    this.zt == 'ybc' ? "已保存" :
                        this.zt == 'ytj' ? "已提交" : this.zt == 'yfk' ? "已反馈" : "未知";
                this.cglx_text = this.cglx == "cgrk" ? "采购入库" : this.cglx == "zddh" ? "直达到货" : "统配采购";
            })
            return data;
        },
        rowStyler:function (index, row) {
            if (row.zt == 'yfk') {
                return 'background-color:yellow';
            }
        },
        onLoadSuccess:function () {
            $(this).datagrid('selectRow', 0);
            cgrk.setBtnStatus();

        },
        onClickRow:function () {
            cgrk.setBtnStatus();
        },
        onSelect:function (rowIndex, rowData) {
            if (rowData) {
                cgrk.setBtnStatus();
            }
        },
        columns:[
            [
                {field:'cgdh', title:'采购单号', align:'center', width:107},
                {field:'cglx_text', title:'采购类型', align:'center', width:61},
                {field:'fhrq', title:'发货日期', align:'center', width:71},
                {field:'gys_jc', title:'供应商名称', align:'center', width:96},
                {field:'cgsl', title:'采购重量', align:'right',
                    formatter:function (value) {
                        var sl = formatSlForHtml(value);
                        return sl;
                    }, width:96},
                {field:'cgje', title:'采购金额(元)', align:'right',
                    formatter:function (value) {
                        var sl = formatJeForHtml2(value);
                        return sl;
                    }, width:111},
                {field:'glfyje', title:'物流费用(元)', align:'right',
                    formatter:function (value) {
                        var sl = formatJeForHtml2(value);
                        return sl;
                    }, width:111},
                {field:'dzck_mc', title:'到货仓库', align:'center', width:75},
                {field:'cggs_jc', title:'采购公司', align:'center', width:67},
                {field:'sbgs_jc', title:'申报公司', align:'center', width:63},
//                {field: 'sbbm_qc', title: '申报部门',align: 'center', width: 59},
                {field:'sbr_xm', title:'申报人', hidden:true},
                {field:'createUser_xm', title:'制单人', hidden:true},
                {field:'yrk', title:'入库状态', width:49, align:'center', formatter:function (value) {
                    if (value == "1")
                        return "已入库";
                    else if (value == "0")
                        return "未入库"
                }},
                {field:'zt_text', title:'状态', align:'center', width:46},
                {field: 'CZZT', title: '导入状态', width: 10,align: 'center',formatter: function (value, row, index) {
                    if (value == 'wdr')
                        return "未导入";
                    if (value == 'drcg')
                        return "导入成功";
                    if (value == 'drsb')
                        return "导入失败";
                    if (value == 'sccg')
                        return "删除成功";
                    if (value == 'scsb')
                        return "删除失败";
                    return "";
                }}
            ]
        ]
    },
    getQuery:function () {
        return {
            "zt":$("[name='querycgrk.zt']").val(),
            "cglx":$("[name='querycgrk.cglx']").val(),
            "cgdwId":$("#cgrk_cgrkw").combotree('getValue'),
            "fhrqStart":$('#zdsj_start').datebox('getValue'),
            "fhrqEnd":$('#zdsj_end').datebox('getValue'),
            "dzckId":$('#cgrk_dzck').combobox('getValue') == null ? null : $('#cgrk_dzck').combotree('getValue'),
            "cgdh":$("[name='querycgrk.cgrkh']").val(),
            "gysid":$("[name='querycgrk.gys']").val(),
            "rkzt":$("[name='querycgrk.rkzt']").val(),
            "dlkhcgd":($("#cgrk_dlkhcgrk").attr("checked")=='checked')
        }
    },
    selectedRowId:null
}

$(function () {
//    //查询条件-加载
    cgrk.initQuery();
//    //按钮-加载
    cgrk.initBtn();
    //列表-加载
    cgrk.initDatagrid();
})

function cgrk_insert() {
    var row = $('#grid_cgrk').datagrid('getSelected');
    if(row){
        var win = $.window({
            title:"采购入库导入",
            width:1000,
            height:300,
            cache:false,
            url:$.webPath() + "/cwgl/cgrk/cgrk_insert?cgrkId="+row.id,
            modal:true,
            buttons:[
                {
                    text:'确认',
                    handler:function () {
                        $(win).mask();
                        $.ajax({
                            url:$.webPath() + "/cwgl/cgrk/generate_voucher",
                            dataType: 'json',
                            type: 'POST',
                            data:{"cgrkSystemDate":$("#cgrk_systemDate").datebox("getValue"),
                                "attachment":$("#cgrk_attachment").val(),
                                "bz":$("#cgrk_bz").val(),
                                "cgrkId":$("#cgrk_id").val()},
                            beforeSend: function () {
                                if($("#cgrk_attachment").val()=="" || $("#cgrk_attachment").val()==null){
                                    $.messager.alert("提示","请填写附单据数", "error");
                                    $(win).mask('hide');
                                    return false;
                                }
                                if($("#cgrk_systemDate").datebox("getValue")=="" || $("#cgrk_systemDate").datebox("getValue")==null){
                                    $.messager.alert("提示","请填写制单时间", "error");
                                    $(win).mask('hide');
                                    return false;
                                }
                            },
                            success:function(msg){
                                $(win).mask('hide');
                                $(win).dialog('close');
                                $("#layout_cgrk").mask("hide");
                                if (msg.responseflag == 0){
                                    $('#grid_cgrk').datagrid('reload');
                                    $.messager.alert("提示", msg.responsemessage, "error");
                                }else {
                                    $('#grid_cgrk').datagrid('reload');
                                    $.messager.show({
                                        title: '提示',
                                        msg: msg.responsemessage,
                                        timeout: 3000,
                                        showType: 'slide'
                                    });
                                }
                            }
                        });
                    }
                }
            ]
        });
    }else{
        $.messager.alert('提示', '请选择一行数据', 'error');
    }
}

//贴现承兑撤销
function cgrk_cx(){
    var row = $('#grid_cgrk').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否撤销该采购入库导入？', function (r) {
            if (r) {
                var index = $('#grid_cgrk').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/cgrk/revoke_voucher?cgrkId="+row.id,
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $("#layout_cgrk").mask();
                    },
                    success:function(msg){
                        $("#layout_cgrk").mask("hide");
                        if (msg.responseflag == 0){
                            $('#grid_cgrk').datagrid('reload');
                            $.messager.alert("提示", msg.responsemessage, "error");
                        } else {
                            $('#grid_cgrk').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: msg.responsemessage,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要撤销的入库单！", "warning");
    }
}

//贴现承兑重置
function cgrk_cz(){
    var row = $('#grid_cgrk').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否重置该入库单？', function (r) {
            if (r) {
                var index = $('#grid_cgrk').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/cgrk/rest_voucher?cgrkId="+row.id,
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $("#layout_cgrk").mask();
                    },
                    success:function(msg){
                        $("#layout_cgrk").mask("hide");
                        if (msg.responseflag == 0){
                            $('#grid_cgrk').datagrid('reload');
                            $.messager.alert("提示", msg.responsemessage, "error");
                        } else {
                            $('#grid_cgrk').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: msg.responsemessage,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要重置的入库单！", "warning");
    }
}

